package com.followme.md5.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.followme.md5.bean.Person;
import com.followme.md5.dao.IPersonDAO;
import com.followme.md5.impl.PersonDAOImpl;

public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String pid = request.getParameter("pid");
			String pwd = request.getParameter("pwd");

			IPersonDAO personDao = new PersonDAOImpl();
			if (personDao.findPersonByName(pid, pwd)) {
				request.getSession().setAttribute("Person", pid);
				List<Person> personList = personDao.getAllPerson();
				request.setAttribute("personList", personList);
				request.getRequestDispatcher("show.jsp").forward(request, response);
			} else {
				response.sendRedirect("error.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

	@Override
	public void destroy() {

		super.destroy();
	}
}
